import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {
    Socket socket = null;

    public TcpEchoClient(String serverip,int port) throws IOException {
        //这里和服务器建立连接就需要知道服务器的地址和端口号，具体如何连接内核会搞定
        socket = new Socket(serverip,port);
    }

    public void start(){
        System.out.println("客户端启动");
        Scanner scanner = new Scanner(System.in);
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream();
            Scanner scannernetwork = new Scanner(inputStream);
            PrintWriter printWriter = new PrintWriter(outputStream)
        ){
           while (true){
               System.out.print("->");
               //从键盘获取请求
               String request = scanner.next();
               //把请求发送到服务器上
               //这里用println，是为了在请求后面加上换行
               //也就是和服务器里的读取scanner.next()相对应
               printWriter.println(request);
               //记得刷新缓冲区
               printWriter.flush();
               //读取响应
               String responce = scannernetwork.next();
               //打印到控制台
               System.out.println(responce);
           }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient client = new TcpEchoClient("192.168.3.184",9090);
        client.start();
    }
}
